package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.misc.Functions;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;
/**
 * 
 * @author Webjoch
 *
 */
public class LargeCrystal extends Puzzle{
	Room room;
	
	public int PRESURE_PAD = 52206;
	public int[] CRYSTAL = {49507, 49510};
	public int[] COLORS = {49495, 49468, 49477, 49486};
	public int[] LODE_STONE = {49471, 49480, 49489, 49498};
	
	private int currentColor = 0;
	private Tile currentPadTile = null;
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
	}

	public GameObject getCrystal(){
		GameObject[] possible = Objects.getLoaded(new Filter<GameObject>(){
			public boolean accept(GameObject o) {
				return room.getArea().contains(o.getLocation()) && Functions.inArray(CRYSTAL, o.getId());
			}
		});
		if (possible.length == 0) return null;
		return possible[0];
	}
	
	@Override
	public boolean isValid() {
		return getCrystal() != null;
	}

	@Override
	public Type getType() {
		return Puzzle.Type.LARGE_CRYSTAL;
	}

	@Override
	public int loop() {
		if (activateAll()) return 1;
		
		//System.out.println("");
		o:
		for (int color : COLORS){
			if (color == COLORS[0]) continue;
			if (getState(color) == getState(COLORS[0])){
				continue;
			}
			Tile location = getPresurePad(color);
			if (location != null){
				for (int i = 0; i < 10; i++){
					if (!Players.getLocal().getLocation().equals(location)){
						if (!location.interact("Walk here")) location.clickOnMap();
					}
					if (Players.getLocal().getLocation().equals(location)) break;
					Task.sleep(1000);
				}
				if (!Players.getLocal().getLocation().equals(location)) break o;
				
				int state = getState(color) > 1 ? getState(color) : getState(color) + 4;
				for (int i = 0; i < 100; i++){
					if ((state + 2 > 4 ? 5 - state + 2 : state + 2) == getState(COLORS[0])){
						Task.sleep(1000);
						if (location != null) new Tile(location.getX()+1, location.getY()+1).click();
						Task.sleep(3000);
						break;
					}
					Task.sleep(50);
				}
			}
			break;
		}
		
		return 0;
	}

	@Override
	public boolean isFinished() {
		return false;
	}
	
	public int getState(int colorId){
		GameObject color = Objects.getNearest(colorId);
		if (color == null) return 0;
		return (int) Calculations.distanceBetween(getCrystal().getLocation(), color.getLocation());
	}
	
	public boolean activateAll(){
		GameObject lodestone = Objects.getNearest(LODE_STONE);
		if (lodestone == null) return false;
		if (Calculations.distanceTo(lodestone) > 5){
			lodestone.getLocation().clickOnMap();
		}else{
			lodestone.interact("Power-up");
			Task.sleep(1000);
		}
		return true;
	}
	
	public Tile getPresurePad(final int colorId){
		GameObject color = Objects.getNearest(colorId);
		if (color == null){
			if (currentColor == colorId){
				return currentPadTile;
			}
			Task.sleep(2000);
			color = Objects.getNearest(colorId);
		}
		GameObject possible = null;
		GameObject[] pads = Objects.getLoaded(new Filter<GameObject>(){
			public boolean accept(GameObject o){
				return o.getId() == PRESURE_PAD;
			}
		});
		for (GameObject pad : pads){
			 if (possible == null){
				 possible = pad;
				 continue;
			 }
			 if (Calculations.distanceBetween(color.getLocation(), pad.getLocation()) < Calculations.distanceBetween(color.getLocation(), possible.getLocation())){
				 possible = pad;
			 }
		}
		currentColor = colorId;
		currentPadTile = possible.getLocation();
		return possible.getLocation();
	}

	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public void messageReceived(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}
}
